<template>
  <div>
    <!-- 按钮 -->

    <el-button style="margin:10px 10px" type="primary" icon="el-icon-refresh-right" circle
      @click="getDynamicExtensionProcessList"></el-button>
    <!-- <el-button type="primary" @click="add()">添加</el-button> -->

    <el-table style="width: 100%;" border :data="list">
      <el-table-column prop="id" label="系统配置id" align="center">
      </el-table-column>
      <el-table-column prop="versionCode" label="版本号" align="center">
      </el-table-column>
      <el-table-column prop="versionIntro" label="版本介绍" align="center">
      </el-table-column>
      <el-table-column prop="downloadUrl" label="下载链接" align="center">
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row, $index }">
          <el-tooltip content="修改" placement="top" slot="reference">
            <el-button type="primary" icon="el-icon-edit" @click="ubpateBannar(row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框  -->
    <el-dialog title="修改配置" :visible.sync="dialogFormVisible">
      <el-form style="width:80%" :model="tmFrom" ref="ruleForm">
        <!--     <el-form-item label="金币" label-width="100px" prop="giveGold">
          <el-input autocomplete="off" v-model="tmFrom.giveGold"></el-input>
        </el-form-item> -->
        <el-form-item label="版本号" label-width="100px" prop="versionCode">
          <el-input autocomplete="off" v-model="tmFrom.versionCode"></el-input>
        </el-form-item>
        <el-form-item label="版本介绍" label-width="100px" prop="versionIntro">
          <el-input autocomplete="off" v-model="tmFrom.versionIntro"></el-input>
        </el-form-item>
        <el-form-item label="下载链接" label-width="100px" prop="downloadUrl">
          <el-input autocomplete="off" v-model="tmFrom.downloadUrl"></el-input>
        </el-form-item>

        <el-form-item label="上传文件" label-width="100px" prop="headimgurl">
          <el-upload class="avatar-uploader" action="https://api.yushiai.com.cn/biz/uploadNormalFile1"
            :headers="headers" name="file" :show-file-list="false" :on-success="handleAvatarSuccess">
            <!-- <img v-if="tmForm.headimgurl" :src="'https://www.yushiai.com.cn/'+tmForm.headimgurl" class="avatar"> -->
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="UpdateBillingRules">修改</el-button>
      </div>
    </el-dialog>

    <!-- 添加对话框  -->
    <el-dialog title="添加配置" :visible.sync="dialogFormVisible1">
      <el-form style="width:80%" :model="list1" :rules="rules1" ref="ruleForm">
        <!-- <el-form-item label="金币" label-width="100px" prop="giveGold">
         <el-input autocomplete="off" v-model="list1.giveGold"></el-input>
       </el-form-item> -->
        <el-form-item label="金币(赠送)" label-width="100px" prop="giveMoreGold">
          <el-input autocomplete="off" v-model="list1.giveMoreGold"></el-input>
        </el-form-item>
        <el-form-item label="充值金额(元)" label-width="100px" prop="rechargeMoney">
          <el-input autocomplete="off" v-model="list1.rechargeMoney"></el-input>
        </el-form-item>
        <!--        <el-form-item label="预计时间/分钟" label-width="100px" prop="estimatedTime">
          <el-input autocomplete="off" v-model="list1.estimatedTime"></el-input>
        </el-form-item>
        <el-form-item label="是否自定义">
          <el-select v-model="list1.isCustom" placeholder="请选择是否自定义" prop="isCustom">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="getAddDynamicExtensionConfiguration">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth';
export default {
  name: 'appVersion',
  data() {

    return {
      // 修改时的数据
      //列表数据
      list: [],
      // 添加列表
      list1: {
        // // 金币花费
        // costGold: "",
        // // 预计时间
        // estimatedTime: "",
        // // 名称
        // intro: "",
        // // 是否自定义
        // isCustom: "",
        // // 预计播放量
        // playNum: ""
      },

      // 对话框显示与隐藏控制
      dialogFormVisible: false,
      dialogFormVisible1: false,
      // 表单对象
      tmFrom: {},
      // 当前为修改还是添加 修改为false 添加为true
      state: true,
      // 表单验证
      rules1: {}
      // rules1: {
      //   intro: [{
      //       required: true,
      //       message: '请输入活动名称',
      //       trigger: 'chang'
      //     },
      //     {
      //       min: 3,
      //       max: 5,
      //       message: '长度在 3 到 5 个字符',
      //       trigger: 'blur'
      //     }
      //   ],
      //   costGold: [{
      //       required: true,
      //       message: '请输入金币花费',
      //       trigger: 'int'
      //     },
      //     // { min: 3, max: 5, message: '请输入金币花费', trigger: 'blur' }
      //   ],
      //   playNum: [{
      //       required: true,
      //       message: '请输入预计播放量',
      //       trigger: 'int'
      //     },
      //     // { min: 1, max: 99999, message: '请输入预计播放量', trigger: 'int' }
      //   ],
      //   estimatedTime: [{
      //       required: true,
      //       message: '请输入预计时间',
      //       trigger: 'change'
      //     },
      //     // { min: 1, max: 9999, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      //   ]
      // },
    }
  },
  mounted() {
    this.getRechargeMoneyList()
  },
  computed: {
    headers() {
      return {
        'session_token': getToken()
      };
    },
  },
  methods: {
    handleAvatarSuccess(res, file) {
      console.log(res, file)
    },
    // 获取推广套餐列表
    async getRechargeMoneyList() {
      let res = await this.$API.system.getConfig()
      // console.log(res.data, "108")
      let arr = []
      var obj = {}; //  一定要在循环体内部创建空对象，每循环一次就创建一个 如果在外部申请对象，得到的永远只有一个
      obj.id = res.data.id;
      obj.versionCode = res.data.versionCode;
      obj.versionIntro = res.data.versionIntro;
      obj.downloadUrl = res.data.downloadUrl;
      arr.push(obj)
      this.list = arr;
      console.log(arr, "108")
    },
    // 刷新
    getDynamicExtensionProcessList() {
      this.getRechargeMoneyList()
    },
    // 添加一条数据窗口弹出
    add() {
      // this.state = true
      this.dialogFormVisible1 = true
      this.list1 = {
        // // 金币花费
        // costGold: "",
        // // 预计时间
        // estimatedTime: "",
        // // 名称
        // intro: "",
        // // 是否自定义
        // isCustom: "",
        // // 预计播放量
        // playNum: ""
      }
    },
    // 添加一条数据发送到后端
    getAddDynamicExtensionConfiguration() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.system.addRechargeMoney(this.list1);
          console.log(res);
          this.dialogFormVisible1 = false;
          this.getRechargeMoneyList()
        } else {
          console.log('error submit!!');
          return false;
        }
      })

      // console.log(this.tmFrom);
    },
    // 点击修改
    ubpateBannar(row) {
      this.state = false
      this.dialogFormVisible = true;
      // 浅拷贝  让tmFrom收到的数据非服务器直接传回的数据
      this.tmFrom = {
        ...row
      };
      console.log(this.tmFrom);
    },
    // 点击修改
    UpdateBillingRules() {
      // console.log(this.id);
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.system.updateConfig(this.tmFrom);
          this.dialogFormVisible = false;
          this.getRechargeMoneyList()
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    // 删除
    async getDelete(id) {
      let res = await this.$API.system.deleteRechargeMoney({
        id: id
      });
      console.log(res);
      this.getRechargeMoneyList()
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
